package com.boarsoft.boar.soagov.nexus;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.boarsoft.bean.ReplyInfo;
import com.boarsoft.boar.entity.SoaNexus;
import com.boarsoft.common.Util;
import com.boarsoft.common.dao.PagedResult;
import com.boarsoft.hibernate.biz.SimpleBizImpl;

@Component
public class NexusBizImpl extends SimpleBizImpl implements NexusBiz {
	@Override
	@Transactional(readOnly = true)
	public PagedResult<SoaNexus> list(String objType0, String objId0, String objType1, String objId1, String key,
			String orderBy, int pageNo, int pageSize) {
		StringBuilder sb = new StringBuilder();
		if (Util.strIsNotEmpty(objType0)) {
			sb.append(" and n.objType0='").append(objType0).append("'");
		}
		if (Util.strIsNotEmpty(objId0)) {
			sb.append(" and n.objId0='").append(objId0).append("'");
		}
		if (Util.strIsNotEmpty(objId1)) {
			sb.append(" and n.objType1='").append(objType1).append("'");
		}
		if (Util.strIsNotEmpty(objId1)) {
			sb.append(" and n.objId1='").append(objId1).append("'");
		}
		if (Util.strIsNotEmpty(key)) {
			sb.append(" and (n.objCode0 like '%").append(key).append("%' or n.objCode1").append(" like '%").append(key)
					.append("%' or n.objName0 like '%").append(key).append("%' or n.objName1").append(" like '%").append(key)
					.append("%')");
		}
		int total = dao.getTotal(SoaNexus.class, sb.toString());
		//
		if (Util.strIsNotEmpty(orderBy)) {
			// String ob = Util.array2str(("e." + orderBy).replaceAll(",",
			// ",").split(","), ",");
			sb.append(" order by ").append(orderBy);
		} else {
			sb.append(" order by n.objId0, n.objId1");
		}
		List<SoaNexus> lt = dao.list(SoaNexus.class, sb.toString(), pageNo, pageSize);
		return new PagedResult<SoaNexus>(total, lt, pageNo, pageSize);
	}

	@Override
	@Transactional(readOnly = true)
	public List<SoaNexus> list(String objType0, String objId0, String objGroup0, //
			String objType1, String objId1, String objGroup1) {
		StringBuilder sb = new StringBuilder();
		if (Util.strIsNotEmpty(objType0)) {
			sb.append(" and objType0='").append(objType0).append("'");
		}
		if (Util.strIsNotEmpty(objId0)) {
			sb.append(" and objId0='").append(objId0).append("'");
		}
		if (Util.strIsNotEmpty(objGroup0)) {
			sb.append(" and objGroup0='").append(objGroup0).append("'");
		}
		if (Util.strIsNotEmpty(objId1)) {
			sb.append(" and objType1='").append(objType1).append("'");
		}
		if (Util.strIsNotEmpty(objId1)) {
			sb.append(" and objId1='").append(objId1).append("'");
		}
		if (Util.strIsNotEmpty(objGroup1)) {
			sb.append(" and objGroup1='").append(objGroup1).append("'");
		}
		return dao.list(SoaNexus.class, sb.toString());
	}

	@Override
	@Transactional(readOnly = true)
	public List<SoaNexus> listByMe(String objId0) {
		StringBuilder sb = new StringBuilder();
		sb.append(" and objId0='").append(objId0).append("'");
		return dao.list(SoaNexus.class, sb.toString());
	}

	@Override
	@Transactional(readOnly = true)
	public List<SoaNexus> listOnMe(String objId1) {
		StringBuilder sb = new StringBuilder();
		sb.append(" and objId1='").append(objId1).append("'");
		return dao.list(SoaNexus.class, sb.toString());
	}

	@Override
	@Transactional(readOnly = true)
	public List<SoaNexus> listByMe(String objId0, String group0) {
		StringBuilder sb = new StringBuilder();
		sb.append(" and objId0='").append(objId0)//
				.append("' and group0='").append(group0).append("'");
		return dao.list(SoaNexus.class, sb.toString());
	}

	@Override
	@Transactional(readOnly = true)
	public List<SoaNexus> listOnMe(String objId1, String group1) {
		StringBuilder sb = new StringBuilder();
		sb.append(" and objId1='").append(objId1)//
				.append("' and group1='").append(group1).append("'");
		return dao.list(SoaNexus.class, sb.toString());
	}

	@Override
	@Transactional
	public void delete(String id) {
		dao.delete(SoaNexus.class, id);
	}

	@Override
	@Transactional
	public ReplyInfo<Object> save(SoaNexus p) {
		StringBuilder sb = new StringBuilder();
		sb.append("select id from ").append(SoaNexus.class.getName());
		sb.append(" where objId0='").append(p.getObjId0());
		sb.append("' and objId1='").append(p.getObjId1());
		sb.append("' and way='").append(p.getWay()).append("'");
		sb.append("' and async=").append(p.getAsync());
		String id = (String) dao.getValue(sb.toString());
		if (Util.strIsNotEmpty(id)) {
			p.setId(id);
			dao.merge(p);
		} else {
			dao.save(p);
		}
		return new ReplyInfo<Object>(true, p.getId());
	}

	@Override
	@Transactional
	public ReplyInfo<Object> update(SoaNexus p) {
		dao.merge(p);
		return new ReplyInfo<Object>(true, p.getId());
	}

	@Override
	@Transactional(readOnly = true)
	public SoaNexus get(String id) {
		return (SoaNexus) dao.get(SoaNexus.class, id);
	}

	@Override
	@Transactional
	public void save(String id, List<SoaNexus> nLt) {
		// 假设 A引用B，B引用C，C引用D，先保存 A->B 的关系
		StringBuilder sb = new StringBuilder();
		sb.append("delete from ").append(SoaNexus.class.getName());
		sb.append(" where objId0='").append(id).append("'");
		dao.executeUpdate(sb.toString());
		dao.batchSave(nLt, 50);
	}

	@Override
	@Transactional(readOnly = true)
	public void getReferences(SoaNexus z, List<SoaNexus> nLt, Map<String, SoaNexus> map) {
		for (SoaNexus n : nLt) {
			if (map.containsKey(n.getObjId1())) {
				continue;
			}
			SoaNexus a = new SoaNexus();
			a.setObjId0(z.getObjId0());
			a.setObjType0(z.getObjType0());
			a.setObjCode0(z.getObjCode0());
			a.setObjName0(z.getObjName0());
			a.setObjVer0(z.getObjVer1());
			//
			a.setObjId1(n.getObjId1());
			a.setObjType1(n.getObjType1());
			a.setObjCode1(n.getObjCode1());
			a.setObjName1(n.getObjName1());
			a.setObjVer1(n.getObjVer1());
			a.setMemo("indirectly");
			map.put(n.getObjId1(), a);
			//
			StringBuilder sb = new StringBuilder();
			sb.append("select o from ").append(SoaNexus.class.getName());
			sb.append(" o where o.objId0='").append(n.getObjId1()).append("'");
			String ql = sb.toString();
			log.debug(ql);
			List<SoaNexus> rLt = dao.list(ql);
			this.getReferences(z, rLt, map);
		}
	}
}